#include "VSMeshDate.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI_NoCreateFun(VSMeshDate,VSObject)
VSMeshDate::VSMeshDate()
{

	m_pVertexBuffer = NULL;
	m_pIndexBuffer = NULL;
	m_pCollapseRecord = NULL;

}
VSMeshDate::~VSMeshDate()
{
	m_pVertexBuffer = NULL;
	m_pIndexBuffer = NULL;
	m_pCollapseRecord = NULL;
}
bool VSMeshDate::Register(VSStream & rStream)const
{
	if (rStream.IsObjectRegister(this))
	{
		return 1;
	}
	if(!VSObject::Register(rStream))
		return 0;

	if(m_pVertexBuffer)
	{
		if(!m_pVertexBuffer->Register(rStream))
			return 0;
	}
	else
		return 0;

	if(m_pIndexBuffer)
	{
		if(!m_pIndexBuffer->Register(rStream))
			return 0;
	}	
	else
		return 0;

	if (m_pCollapseRecord)
	{
		if (!m_pCollapseRecord->Register(rStream))
		{
			return 0;
		}
	}
	return 1;
}
unsigned int VSMeshDate::DiskUse()const
{
	unsigned int Used = VSObject::DiskUse();
	Used += sizeof(VSVertexBuffer *);

	Used += sizeof(VSIndexBuffer *);

	Used += sizeof(VSCollapseRecord *);
	return Used;
}
bool VSMeshDate::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSObject::Save(rStream,iSaveUse))
		return 0;


	VSVertexBuffer * Temp = m_pVertexBuffer;
	if(!Temp)
		return 0;
// 	if(!rStream.Write(&Temp,sizeof(VSVertexBuffer *)))
// 		return 0;
	if(!rStream.WriteObjectPtr(m_pVertexBuffer))
		return 0;
	iSaveUse += sizeof(VSVertexBuffer *);



	VSIndexBuffer * Temp1 = m_pIndexBuffer;
	if(!Temp1)
		return 0;
// 	if(!rStream.Write(&Temp1,sizeof(VSIndexBuffer *)))
// 		return 0;
	if(!rStream.WriteObjectPtr(m_pIndexBuffer))
		return 0;
	iSaveUse += sizeof(VSIndexBuffer *);

	
// 	VSCollapseRecord * pCollapseRecord = m_pCollapseRecord;
// 	if(!rStream.Write(&pCollapseRecord,sizeof(VSCollapseRecord *)))
// 		return 0;
	if(!rStream.WriteObjectPtr(m_pCollapseRecord))
		return 0;
	iSaveUse += sizeof(VSCollapseRecord *);
	return 1;
}
bool VSMeshDate::Load(VSStream & rStream,unsigned int &iSaveUse)
{
	if(!VSObject::Load(rStream,iSaveUse))
		return 0;



// 	VSVertexBuffer * Temp = NULL;
// 	if(!rStream.Read(&Temp,sizeof(VSVertexBuffer *)))
// 		return 0;
// 	AddLink(Temp);
	if(!rStream.ReadObjectPtr(m_pVertexBuffer))
		return 0;
	iSaveUse += sizeof(VSVertexBuffer *);



// 	VSIndexBuffer * Temp1 = NULL;
// 	if(!rStream.Read(&Temp1,sizeof(VSIndexBuffer *)))
// 		return 0;
// 	AddLink(Temp1);
	if(!rStream.ReadObjectPtr(m_pIndexBuffer))
		return 0;
	iSaveUse += sizeof(VSIndexBuffer *);

// 	VSCollapseRecord * pCollapseRecord = NULL;
// 	if(!rStream.Read(&pCollapseRecord,sizeof(VSCollapseRecord *)))
// 		return 0;
// 	AddLink(pCollapseRecord);
	if(!rStream.ReadObjectPtr(m_pCollapseRecord))
		return 0;
	iSaveUse += sizeof(VSCollapseRecord *);

	return 1;
}
bool VSMeshDate::Link(VSStream & rStream)
{
	if(!VSObject::Link(rStream))
		return 0;

	//m_pVertexBuffer = (VSVertexBuffer*)rStream.GetMapValue(GetNextLink());
	rStream.LinkObjectPtr(m_pVertexBuffer);
	if(!m_pVertexBuffer)
		return 0;

	//m_pIndexBuffer = (VSIndexBuffer *)rStream.GetMapValue(GetNextLink());
	rStream.LinkObjectPtr(m_pIndexBuffer);
	if(!m_pIndexBuffer)
		return 0;

	//m_pCollapseRecord = (VSCollapseRecord *)rStream.GetMapValue(GetNextLink());
	rStream.LinkObjectPtr(m_pCollapseRecord);

	return 1;
}
bool VSMeshDate::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSMeshDate* Temp = DynamicCast<VSMeshDate>(pObject); 
	if(!Temp)
		return 0;
	if(!Temp->m_pIndexBuffer)
		return 0;
	if(!Temp->m_pVertexBuffer)
		return 0;
	if(VSObject::Clone(pObject,uiType))
	{

		if(Temp->m_pVertexBuffer)
		{
			VSVertexBuffer * pTemp1  = Temp->m_pVertexBuffer;
			VSVertexBuffer * pTemp2 =(VSVertexBuffer *)VSObject::GetInstance((pTemp1)->GetType().GetName());
			pTemp2->Clone(pTemp1,uiType);
			m_pVertexBuffer = pTemp2;
		}
		else
		{
			m_pVertexBuffer = NULL;
		}


		if(Temp->m_pIndexBuffer)
		{
			VSIndexBuffer * pTemp1  = Temp->m_pIndexBuffer;
			VSIndexBuffer * pTemp2 =(VSIndexBuffer *)VSObject::GetInstance((pTemp1)->GetType().GetName());
			pTemp2->Clone(pTemp1,uiType);
			m_pIndexBuffer = pTemp2;
		}
		else
		{
			m_pIndexBuffer = NULL;
		}

		if (Temp->m_pCollapseRecord)
		{
			VSCollapseRecord * pTemp1  = Temp->m_pCollapseRecord;
			VSCollapseRecord * pTemp2 =(VSCollapseRecord *)VSObject::GetInstance((pTemp1)->GetType().GetName());
			pTemp2->Clone(pTemp1,uiType);
			m_pCollapseRecord = pTemp2;
		}
		else
		{
			m_pCollapseRecord = NULL;
		}
		return 1;

		

	}
	else
	{
		return 0;
	}
}
bool VSMeshDate::SetIndexBuffer(VSIndexBuffer * pIndexBuffer)
{
	if(!pIndexBuffer)
		return 0;

	if(pIndexBuffer->GetNum())
	{
		m_pIndexBuffer = pIndexBuffer;
		return 1;

	}
	else
	{
		return 0;
	}
}
bool VSMeshDate::SetVertexBuffer(VSVertexBuffer *pVertexBuffer)
{
	if(!pVertexBuffer)
		return 0;
	if(pVertexBuffer->GetVertexNum())
	{
		m_pVertexBuffer = pVertexBuffer;
		return 1;
	}
	else
	{
		return 0;
	}
}
void VSMeshDate::SetLodDesirePercent(VSREAL fDesirePercent)
{
	if (m_pCollapseRecord)
	{
		if (m_pCollapseRecord->GetRecordNum() > 0)
		{
			unsigned int uiDesireRecordID = (unsigned int )( (1.0f- fDesirePercent) * (m_pCollapseRecord->GetRecordNum() - 1));
			m_pCollapseRecord->SetDesireRecordID(uiDesireRecordID);
		}
		
	}

}